package com.jyx.project.controller;

import com.jyx.common.response.AppResponse;
import com.jyx.project.utils.OssTemplate;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/project")
@Api(tags = "项目模块,用来完成文件上传")
public class ProjectController {

    @Autowired
    private OssTemplate ossTemplate;

    @PostMapping("/upload")
    public AppResponse<Map<String,Object>> upload(@RequestParam("file")MultipartFile[] files) throws IOException {
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> filesUrl = new ArrayList<>();
        if (files != null && files.length > 0){
            for (MultipartFile file : files) {
                String fileUrl = ossTemplate.upload(file.getInputStream(), file.getOriginalFilename());
                filesUrl.add(fileUrl);
            }
        }
        map.put("urls",filesUrl);
        return AppResponse.ok(map);
    }
}
